/*
 * MainForm.java
 *
 * Created on 21 novembre 2007, 21:45
 */
package bbos;

import bbos.General.Model.mMatch;
import bbos.General.Model.mPlayerType;
import bbos.General.Model.mPlayer;
import bbos.General.Model.mTeam;
import bbos.General.Views.*;
import bbos.General.Views.Match.jdgChallenge;
import bbos.General.Views.Rosters.*;
import bbos.General.Views.MyBBoS.*;
import bbos.General.Views.Team.jdgTeam;
import bbos.Tools.sdLang;
import bbos.Tools.sdSettings;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.Vector;

/**
 *
 * @author  Administrateur
 */
public class MainForm extends javax.swing.JFrame {

    static MainForm _singleton;
    jpnMyTeam _panel = null;
    mTeam _team = null;

    /** Creates new form MainForm */
    public MainForm() {
        initComponents();
        setPreferredSize(new java.awt.Dimension(800, 600));
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        DisplayMode dmode = gs.getDisplayMode();

        if (dmode != null) {
            int screenWidth = dmode.getWidth();
            int screenHeight = dmode.getHeight();
            this.setLocation((screenWidth - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);
        }



        mBBoS instance = mBBoS.getSingleton();
        Vector teams = instance.getMyTeams();
        for (int i = 0; i < teams.size(); i++) {
            mTeam team = (mTeam) teams.get(i);
            JRadioButtonMenuItem menu = new JRadioButtonMenuItem(team.getName());
            menu.setActionCommand(Integer.toString(team.getId()));
            menu.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
                    item.getModel().setSelected(true);
                    MyTeamsGroup.setSelected(item.getModel(), true);
                    repaint();
                    updateMenuStates();
                }
            });

            if (i == 0) {
                menu.setSelected(true);
                _team = team;
            }
            MyTeamsGroup.add(menu);
            jmnMyTeams.add(menu);
        }
        repaint();
        updateMenuStates();
        jdgProgressBar.setProgressValue(4, "Done");
        
        paintLocale();
    }

    public void refresh() {
        if (_panel != null) {
            _panel.refresh();
        }
    }

    public void redraw() {
        jmnMyTeams.removeAll();
        while (MyTeamsGroup.getElements().hasMoreElements()) {
            MyTeamsGroup.remove(MyTeamsGroup.getElements().nextElement());
        }

        mBBoS instance = mBBoS.getSingleton();
        Vector teams = instance.getMyTeams();
        for (int i = 0; i < teams.size(); i++) {
            mTeam team = (mTeam) teams.get(i);
            JRadioButtonMenuItem menu = new JRadioButtonMenuItem(team.getName());
            menu.setActionCommand(Integer.toString(team.getId()));
            menu.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
                    item.getModel().setSelected(true);
                    MyTeamsGroup.setSelected(item.getModel(), true);
                    repaint();
                    updateMenuStates();
                }
            });
            MyTeamsGroup.add(menu);
            jmnMyTeams.add(menu);
        }

        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MyTeamsGroup = new javax.swing.ButtonGroup();
        mainMenuBar = new javax.swing.JMenuBar();
        jmnFile = new javax.swing.JMenu();
        jmnMyTeams = new javax.swing.JMenu();
        jmnTeamManagement = new javax.swing.JMenu();
        jmiNewTeam = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jmiBuyPlayer = new javax.swing.JMenuItem();
        jmiBuyStaff = new javax.swing.JMenuItem();
        jmiRetirePlayer = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jmiAbandonTeam = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        jmiChallenge = new javax.swing.JMenuItem();
        jmnRules = new javax.swing.JMenu();
        jmiAllowedRosters = new javax.swing.JMenuItem();
        jmnHelp = new javax.swing.JMenu();
        jmiAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(950, 700));

        jmnFile.setText("File");
        mainMenuBar.add(jmnFile);

        jmnMyTeams.setText("My Teams");
        mainMenuBar.add(jmnMyTeams);

        jmnTeamManagement.setText("Team Management");

        jmiNewTeam.setText("New Team");
        jmiNewTeam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiNewTeamActionPerformed(evt);
            }
        });
        jmnTeamManagement.add(jmiNewTeam);
        jmnTeamManagement.add(jSeparator1);

        jmiBuyPlayer.setText("Buy Player");
        jmiBuyPlayer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiBuyPlayerActionPerformed(evt);
            }
        });
        jmnTeamManagement.add(jmiBuyPlayer);

        jmiBuyStaff.setText("Buy Staff");
        jmiBuyStaff.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiBuyStaffActionPerformed(evt);
            }
        });
        jmnTeamManagement.add(jmiBuyStaff);

        jmiRetirePlayer.setText("Retire player");
        jmiRetirePlayer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiRetirePlayerActionPerformed(evt);
            }
        });
        jmnTeamManagement.add(jmiRetirePlayer);
        jmnTeamManagement.add(jSeparator2);

        jmiAbandonTeam.setText("Abandon team");
        jmiAbandonTeam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiAbandonTeamActionPerformed(evt);
            }
        });
        jmnTeamManagement.add(jmiAbandonTeam);
        jmnTeamManagement.add(jSeparator3);

        jmiChallenge.setText("Challenge a team");
        jmiChallenge.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiChallengeActionPerformed(evt);
            }
        });
        jmnTeamManagement.add(jmiChallenge);

        mainMenuBar.add(jmnTeamManagement);

        jmnRules.setText("Rules");

        jmiAllowedRosters.setText("Allowed Rosters");
        jmiAllowedRosters.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiAllowedRostersActionPerformed(evt);
            }
        });
        jmnRules.add(jmiAllowedRosters);

        mainMenuBar.add(jmnRules);

        jmnHelp.setText("?");

        jmiAbout.setText("Item");
        jmiAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiAboutActionPerformed(evt);
            }
        });
        jmnHelp.add(jmiAbout);

        mainMenuBar.add(jmnHelp);

        setJMenuBar(mainMenuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jmiNewTeamActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jmiNewTeamActionPerformed
    {//GEN-HEADEREND:event_jmiNewTeamActionPerformed
        jdgTeam jfr = new jdgTeam();
        jfr.setVisible(true);
    }//GEN-LAST:event_jmiNewTeamActionPerformed

    private void jmiRetirePlayerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiRetirePlayerActionPerformed
        if (_team != null) {
            if (_panel.getSelectedPlayer() != null) {
                mPlayer player = _panel.getSelectedPlayer();
                int option = JOptionPane.showConfirmDialog(this, "You will retire " + player.getName() + " from " + _team.getName() + ". Are you sure ?", "Retire player", JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION) {
                    mBBoS.getSingleton().retirePlayer(player);
                    _team.retirePlayer(player);
                }
            }
        }
        repaint();
        
    }//GEN-LAST:event_jmiRetirePlayerActionPerformed

    private void jmiAbandonTeamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiAbandonTeamActionPerformed

        if (_team != null) {
            int option = JOptionPane.showConfirmDialog(this, "You will abbandon " + _team.getName() + ". Are you sure ?", "Abandon team", JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                mBBoS.getSingleton().removeTeam(_team);                
                _team=null;                
                redraw();
            }
        }
        repaint();
    }//GEN-LAST:event_jmiAbandonTeamActionPerformed

    private void jmiBuyStaffActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiBuyStaffActionPerformed

        jdgBuyStaff window = new jdgBuyStaff(_team);
        window.setVisible(true);
        
    }//GEN-LAST:event_jmiBuyStaffActionPerformed

    private void jmiBuyPlayerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiBuyPlayerActionPerformed

        jdgBuyPlayer window = new jdgBuyPlayer(_team);
        window.setVisible(true);

    }//GEN-LAST:event_jmiBuyPlayerActionPerformed

    private void jmiChallengeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiChallengeActionPerformed

        jdgChallenge window = new jdgChallenge(_team);
        window.setVisible(true);
        
    }//GEN-LAST:event_jmiChallengeActionPerformed

    private void jmiAllowedRostersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiAllowedRostersActionPerformed
        jdgRosters window = new jdgRosters();
        window.setVisible(true);
    }//GEN-LAST:event_jmiAllowedRostersActionPerformed

    private void jmiAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiAboutActionPerformed
       jdgAboutBox window=new jdgAboutBox();
       window.setVisible(true);
    }//GEN-LAST:event_jmiAboutActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup MyTeamsGroup;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JMenuItem jmiAbandonTeam;
    private javax.swing.JMenuItem jmiAbout;
    private javax.swing.JMenuItem jmiAllowedRosters;
    private javax.swing.JMenuItem jmiBuyPlayer;
    private javax.swing.JMenuItem jmiBuyStaff;
    private javax.swing.JMenuItem jmiChallenge;
    private javax.swing.JMenuItem jmiNewTeam;
    private javax.swing.JMenuItem jmiRetirePlayer;
    private javax.swing.JMenu jmnFile;
    private javax.swing.JMenu jmnHelp;
    private javax.swing.JMenu jmnMyTeams;
    private javax.swing.JMenu jmnRules;
    private javax.swing.JMenu jmnTeamManagement;
    private javax.swing.JMenuBar mainMenuBar;
    // End of variables declaration//GEN-END:variables
    
    protected void paintLocale()
    {
        jmnFile.setText(sdLang.getSingleton().getResource("File"));
        jmnMyTeams.setText(sdLang.getSingleton().getResource("My_Teams"));
        jmnRules.setText(sdLang.getSingleton().getResource("Rules"));
        jmnTeamManagement.setText(sdLang.getSingleton().getResource("Team_Management"));
        jmiAbandonTeam.setText(sdLang.getSingleton().getResource("Abandon_team"));
        jmiNewTeam.setText(sdLang.getSingleton().getResource("New_Team"));
        jmiChallenge.setText(sdLang.getSingleton().getResource("Challenge_a_team"));
        jmiBuyStaff.setText(sdLang.getSingleton().getResource("Buy_Staff"));
        jmiBuyPlayer.setText(sdLang.getSingleton().getResource("Buy_Player"));
        jmiRetirePlayer.setText(sdLang.getSingleton().getResource("Retire_Player"));
        jmiAllowedRosters.setText(sdLang.getSingleton().getResource("Allowed_rosters"));
        
        pack();
    }
    
    public void paint(Graphics g) {
        super.paint(g);
        if (MyTeamsGroup.getSelection() != null) {
            int team_id = Integer.valueOf(MyTeamsGroup.getSelection().getActionCommand());
            int old_id = 0;
            if (_team != null) {
                old_id = _team.getId();
            }
            if ((team_id != old_id) || (_panel == null)) {
                if (_panel != null) {
                    this.remove(_panel);
                }
                Vector teams = mBBoS.getSingleton().getMyTeams();
                for (int i = 0; i < teams.size(); i++) {
                    if (((mTeam) teams.get(i)).getId() == team_id) {
                        _team = (mTeam) teams.get(i);
                    }
                }
                _panel = new jpnMyTeam(_team);
                this.add(_panel, BorderLayout.CENTER);
                this.pack();
                _panel.repaint();

            }
        }
    }

    public void updateMenuStates() {

        jmiAbandonTeam.setEnabled(false);
        jmiBuyPlayer.setEnabled(false);
        jmiBuyStaff.setEnabled(false);
        jmiRetirePlayer.setEnabled(false);
        jmiChallenge.setEnabled(false);

        /* If active tab is MyBBoS*/

        if (_team != null) {
            jmiAbandonTeam.setEnabled(true);
            jmiChallenge.setEnabled(true);
            for (int i = 0; i < _team.getMatches().size(); i++) {
                mMatch match = (mMatch) _team.getMatches().get(i);
                if ((match.getState() == mMatch.STATE_IN_COURSE) || (match.getState() == mMatch.STATE_CHALLENGE)) {
                    jmiChallenge.setEnabled(false);
                }
            }


            if (_team.getTreasury() >= 10000) {
                jmiBuyStaff.setEnabled(true);
            }

            Vector v = _team.getAvailablePlayersTypes();
            int minPrice = Integer.MAX_VALUE;
            for (int i = 0; i < v.size(); i++) {
                mPlayerType type = (mPlayerType) v.get(i);
                minPrice = Math.min(minPrice, type.getCost());
            }
            if (_team.getTreasury() >= minPrice) {
                jmiBuyPlayer.setEnabled(true);
            }
            if (_panel != null) {
                if (_panel.getSelectedPlayer() != null) {
                    jmiRetirePlayer.setEnabled(true);
                }
            }
        }
    }

    public static MainForm getSingleton() {
        if (_singleton == null) {
            _singleton = new MainForm();
        }
        return _singleton;
    }
}
